'use strict';
Scene.create('main', function() {
	let { screenSize, netmap, CameraMoveingObject, Joystick } = global_ns;
	let { BrickNode } = brick_ns;
	
	
	let cameraMoveingObject = new CameraMoveingObject(main.camera);
	
	let resizeHandler = e => {
		netmap.size.set(screenSize);
	};
	
	resizeHandler();
	cvs.on('resize', resizeHandler);
	
	
	let bricks = [], bricksBuffer = [];
	
	let createBrickOnTouch = (touch, arr) => {
		let w = 25;
		
		if(arr.find(i => i.pos.isSame(pos))) return;
		
		arr.push(new BrickNode({ pos, size: vec2(w*2, w) }));
	};
	
	
	let touch = null;
	let clicked = false;
	
	let touchClick = touch => {
		createBrickOnTouch(touch, bricks);
	};


	//===============load===============//
	this.load = () => console.log('loaded');

	//===============init===============//
	this.init = () => console.log('inited');


	//===============updata===============//
	this.updata = function(dt) {
		//=======prePROCES=======//--vs--//=======EVENTS=======//
		cameraMoveingObject.updata(touches, main.camera);
		//==================================================//


		//=======PROCES=======//--vs--//=======UPDATA=======//
	/*	let pos = vec2(touch.x, touch.y).plus(main.camera).div(w).floor().inc(w);
		
		if(touch = touches.findTouch()||touch) {
			if(touch.isPress()) clicked = true;
			
			if(touch.isMove() && touch.isTimeDown(5)) clicked = false;
			
			if(clicked && touch.isUp()) {
				touchClick(touch);
				clicked = false;
			};
			
			
			if(touch.isTimeDown(20)) {
				console.log(5);
				let l = bricks.findIndex(i => i.pos.isSame(pos));
				if(~l) bricks.splice(l, 1);
			//	createBrickOnTouch(touch, bricksBuffer);
			};
		};*/
		//==================================================//


		//==========DRAW==========//--vs--//==========RENDER==========//
		main.ctx.clearRect(0, 0, cvs.width, cvs.height);
		
		for(let i = 0; i < bricks.length; i++) bricks[i].draw(main);
		for(let i = 0; i < bricksBuffer.length; i++) bricksBuffer[i].draw(main);
		
		netmap.draw(main);
		
		global_ns.selectionRect.pos1.set(50, 50);
		global_ns.selectionRect.pos2.set(100, 100);
		global_ns.selectionRect.draw(main);
		
		main.save();
		main.beginPath();
		main.strokeStyle = '#ffff00';
		
		main.moveTo(0, 0);
		main.lineTo(300, 300);
	//	main.stroke();
		main.restore();
	}; //==============================//
	
	
	this.exit = () => console.log('exited');
});


Scene.run('main');
